//
//  DetailViewController.m
//  小胖兔旅行指南
//
//  Created by dlios on 15/4/15.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "DetailViewController.h"
#import "MyConnection.h"
#import "UIImageView+WebCache.h"

@interface DetailViewController () <UIScrollViewDelegate>

{
    UIScrollView *_scroll;
    UIPageControl *_page;
}

@property (nonatomic, retain) NSMutableDictionary *dic;
@property (nonatomic, retain) UIScrollView *secondScroll;

@end

@implementation DetailViewController

-(void)dealloc
{
    [_dic release];
    [_secondScroll release];
    
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dic = [NSMutableDictionary dictionary];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     _scroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
    [self.view addSubview:_scroll];
    [_scroll setBackgroundColor:[UIColor whiteColor]];
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 2);
    
    NSString *str = [NSString stringWithFormat:@"%@/%ld", Recomm, self.tag];
    [MyConnection connectWithUrl:str parmaters:nil finishBlock:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dic = dic;
        
        [self showAll];
        
        NSString *titleName = [_dic objectForKey:@"title"];
        self.title = titleName;
    }];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang.png"] style:UIBarButtonItemStylePlain target:self action:@selector(collectClick:)];
}

- (void)collectClick:(id)sender
{
    
}

- (void)showAll
{
    [self showScrollView];
    [self showText];
}

- (void)showScrollView
{
    self.secondScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _scroll.frame.size.width, 200)] autorelease];
    NSMutableArray *array = [_dic objectForKey:@"bg_pic"];
    _secondScroll.contentSize = CGSizeMake(_scroll.frame.size.width * [array count], 200);
    _secondScroll.bounces = NO;
    _secondScroll.pagingEnabled = YES;
    _secondScroll.delegate = self;
    
    
    for (int i = 1; i <= [array count]; i++) {
        UIScrollView *smallScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake((i - 1) * _secondScroll.frame.size.width, 0, _secondScroll.frame.size.width, _secondScroll.frame.size.height)] autorelease];
        smallScroll.delegate = self;
        [_secondScroll addSubview:smallScroll];
        
        UIImageView *aImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _secondScroll.frame.size.width, 200)] autorelease];
        [aImage sd_setImageWithURL:[NSURL URLWithString:[array objectAtIndex:i - 1]]];
        [smallScroll addSubview:aImage];
    }
    
    [_scroll addSubview:_secondScroll];
    
    _page = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, _scroll.frame.size.width, 10)] autorelease];
    _page.numberOfPages = [array count];
    _page.backgroundColor = [UIColor grayColor];
    [_scroll addSubview:_page];
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)pageAction:(UIPageControl *)sender
{
    NSInteger num = sender.currentPage;
    _secondScroll.contentOffset = CGPointMake(num * _secondScroll.frame.size.width, 0);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _secondScroll) {
        CGPoint point = scrollView.contentOffset;
        NSInteger num = point.x / _secondScroll.frame.size.width;
        _page.currentPage = num;
    }
}

- (void)showText
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"iconfont-didian" ofType:@"png"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    UIImage *image = [UIImage imageWithData:data];
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 230, 50, 50)] autorelease];
    [imageView setBackgroundColor:[UIColor whiteColor]];
    [_scroll addSubview:imageView];
    [imageView setImage:image];
    
    UIView *aView = [[[UIView alloc] initWithFrame:CGRectMake(60, 230, self.view.frame.size.width - 60, 50)] autorelease];
    [aView setBackgroundColor:[UIColor whiteColor]];
    [_scroll addSubview:aView];
    
    UILabel *destination = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, aView.frame.size.width, 20)] autorelease];
    [aView addSubview:destination];
    [destination setText:[_dic objectForKey:@"destination"]];
    
    UILabel *address = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, aView.frame.size.width, 30)] autorelease];
    [aView addSubview:address];
    [address setText:[_dic objectForKey:@"address"]];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    NSString *details = [_dic objectForKey:@"details"];
    CGRect rect = [details boundingRectWithSize:CGSizeMake(_scroll.frame.size.width - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    UILabel *contents = [[[UILabel alloc] initWithFrame:CGRectMake(5, 300, _scroll.frame.size.width - 10, rect.size.height)] autorelease];
    [contents setBackgroundColor:[UIColor whiteColor]];
    [contents setText:details];
    contents.numberOfLines = 0;
    [_scroll addSubview:contents];
    
    UILabel *cost = [[[UILabel alloc] initWithFrame:CGRectMake(5, contents.frame.origin.y + contents.frame.size.height + 20, contents.frame.size.width, 30)] autorelease];
    NSString *price = [NSString stringWithFormat:@"费用: %@", [_dic objectForKey:@"price"]];
    [cost setText:price];
    [_scroll addSubview:cost];
    
    
    UILabel *num = [[[UILabel alloc] initWithFrame:CGRectMake(5, cost.frame.origin.y + cost.frame.size.height + 20, cost.frame.size.width, 30)] autorelease];
    [num setText:@"联系方式"];
    [num setFont:[UIFont systemFontOfSize:20]];
    [_scroll addSubview:num];
    
    
    NSMutableDictionary *dictel = [NSMutableDictionary dictionary];
    [dictel setObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    NSString *telString = [_dic objectForKey:@"contact"];
    CGRect rectTel = [telString boundingRectWithSize:CGSizeMake(num.frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictel context:nil];
    UILabel *tel = [[[UILabel alloc] initWithFrame:CGRectMake(5, num.frame.origin.y + num.frame.size.height + 10, num.frame.size.width, rectTel.size.height)] autorelease];
    [tel setText:telString];
    tel.numberOfLines = 0;
    [_scroll addSubview:tel];
    
    [_scroll setContentSize:CGSizeMake(0, tel.frame.origin.y + tel.frame.size.height + 64 + 49 + 50)];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
